package net.zjitc.reflect.ex01;

import net.zjitc.reflect.Person;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;

/**
 * Created by Administrator on 2017/11/6.
 */
public class ReflectReview {

    //找ex02包下面的所有的类

    /**
     * 1. 找到ex02的包
     * 2.通过File对象，找ex02下面的所有子文件
     * 3.获得文件名（类名）ex02.Admin  ex02.User
     */
    /**
     * 类加载
     * @param args
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws NoSuchFieldException
     * @throws InvocationTargetException
     */
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, InvocationTargetException {
        //空字符串是根路径
        //ex02的包的完整的名字是net.zjitc.reflect.exo2,将此转成路径资源书写方式net/zjitc/reflect/ex02
        String pageName = "net.zjitc.reflect.ex02";
        String pagePath= pageName.replace(".","/");
        System.out.println("pagePath = "+pagePath);

        URL resource = Thread.currentThread().getContextClassLoader().getResource(pagePath);
        System.out.println(resource.getPath());
        System.out.println("------------------");
        //创建ex02文件对象
        File file = new File(resource.getPath());
        //找到文件下的所有子文件及文件夹
        File[] files = file.listFiles();
        for (File f :files){
            //完整类名
            String className = pageName+"."+f.getName();
            className = className.substring(0,className.length()-6);//去掉了.class后缀
            //System.out.println(className);
            Class clazz = Class.forName(className);
            Constructor[] constructors = clazz.getConstructors();
            for(Constructor c :constructors){
                System.out.println(c.getName());
            }
            //System.out.println(clazz.newInstance());
        }
        /**
         * 找到ex02下的所有子文件
         */

        System.out.println("-------------end----------");
        System.out.println(resource);
       /* Class clazz = Class.forName("net.zjitc.reflect.Person");


        // 通过类对象，1 创建实例
        Person p = (Person) clazz.newInstance();
       // p.speak()
        //通过类对象，2. 可以获得这个类的相关内容
        Constructor[] constructors = clazz.getConstructors();
        //获得的成员变量
        Field age = clazz.getDeclaredField("age");
        System.out.println(age);
        //获得方法
        Method[] methods = clazz.getMethods();
        for (Method m : methods){
            if (m.getName().equals("speak")){
                System.out.println("speak调用。。。。。。。。。。");
                m.invoke(p,"zhs",21);
            }
            //System.out.println(m);
        }*/
    }
}
